<?php
/**
 * Created by PhpStorm.
 * User: jacob
 * Date: 18-10-12
 * Time: 下午4:04
 */
namespace Common;

abstract class JsonApi extends Api{
  protected function before(Request $request, Response $response):bool {
    if (empty($request->data)){
      $request->data = new \stdClass();
    }else{
      $request->data = json_decode($request->data);
    }

    if ($request->data == false){
      $response->httpStatus = HttpStatus::FORMAT_ERROR;
      $response->httpMessage = "JsonApi before json_decode error";
      return false;
    }
    return true;
  }

  public function after(Request $request, Response $response) {
    if (empty($response->data)){
      return;
    }
    $response->data = json_encode($response->data);
    if ($response->data == false){
      $response->httpStatus = HttpStatus::FORMAT_ERROR;
      $response->httpMessage = "JsonApi after json_encode error";
      return;
    }
  }
}